package com.tefl.back.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tefl.entity.Administrator;
import com.tefl.service.AdministratorService;

/**
 * 管理员Action
 * 
 * @author liuxin
 * 
 */
public class AdminAction extends ActionSupport {

	private Administrator admin;

	@Resource
	private AdministratorService adminService;

	/**
	 * 处理管理员登陆
	 * 
	 * @return
	 */
	public String login() {
		String result = null;
		PrintWriter res = null;
		
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			res = ServletActionContext.getResponse().getWriter();
		} catch (IOException e1) {
			result = ActionSupport.ERROR;
			e1.printStackTrace();
		}

		try {
			admin = adminService.login(admin.getAdmin_name(), admin.getAdmin_password());
			
			ServletActionContext.getRequest().getSession().setAttribute("admin", admin);
			res.write("{success:true,msg:'OK'}");
			
			result = ActionSupport.SUCCESS;
		} catch (Exception e) {
			res.write("{success:true,msg:'"+e.getMessage()+"'}");
			result = ActionSupport.ERROR;
		} finally {
			res.close();
		}

		return null;
	}

	public Administrator getAdmin() {
		return admin;
	}

	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}

}
